page=1;
query=[];
height=4;
width=3;
$( function()
{
    set_grid(width,height);
    get_page(page);
}
);
$(window).scroll(function(){
    if ($(window).scrollTop() > 450)
    {
        $("#next").css({"position": "absolute","top":700});
        $("#prev").css({"position": "absolute","top":700});
    }
    else
    {
        $("#next").css({"position": "fixed","top":250});
        $("#prev").css({"position": "fixed","top":250});
    }
});
function set_grid(width,height)
{
    var grid_width=width*160+10;
    var grid_height=height*210+10;
    $('#grid').css({'width':grid_width, 'height':grid_height});
    var grid_position=$('#grid').offset();
    $('#prev').css({'left': grid_position.left-125});
    $('#next').css({'left': grid_position.left+grid_width+5});
}
function get_page(p)
{
    $('#grid').css({'opacity': '0'});
    $('#grid').animate({opacity: 1}, 2000);
    $('#grid').html('<span class="anchor"></span>');
    var next=p+1;
    var prev=p-1;
    $("#next").attr('onclick','get_page('+next+')');
    $("#prev").attr('onclick','get_page('+prev+')');
    page=p;
    if (page-1==0)
    {
        $("#prev").hide();
    }
    else
    {
        $("#prev").show();
    }
    $.post(
        '/ajax/getpage',
        {
            page:p,
            grid_length:width*height
        },
        function (data)
        {
            //alert(data);

            max_pages=data.pages;
            if (page==max_pages)
            {
                $("#next").hide();
            }
            else
            {
                $("#next").show();
            }

            $.each(data.data,
                function(index,value)
                {
                    $('.anchor').before('<div><img src="/public/css/img/loading.gif" data-load_id="'+value.id+'"><img onclick="show_image('+value.id+')" src="'+value.path_to_thumb+'"  data-id="'+value.id+'" style="display:none">');
                    $('#grid div img:eq(1)').on('load',add_to_query(value.id));
                }
            );
            check=setInterval(check_query,200);

        }
    );

}
function add_to_query(id)
{
    query.push(id);
}

function check_query()
{
    if (query.length==$('#grid div').size())
    {

        clearInterval(check);
        show_images();
    }
}

function show_rnd_image()
{
    var rnd_max=query.length;
    var rnd_num=Math.floor(Math.random()*(rnd_max+1));
    var id=query[rnd_num];
    $('img[data-load_id="'+id+'"]').hide();
    $('img[data-id="'+id+'"]').fadeIn(400);
    query.splice(rnd_num,1);
}

function show_images()
{
    timer=setInterval(show_rnd_image, 150);
    if (query.length<1) clearInterval(timer);
}

function show_image(id)
{
    $.post(
        '/ajax/getimageinfo',
        {
            id:id
        },
        function (data)
        {
            //alert(data);
            $.colorbox(
                {
                    href:data.path_to_fullsize,
                    title:data.description
                }
            );
        }
    );
}

function chkForm()
{
    if ($('#add_image input[type="file"]').val()=='')
    {
        alert ('Вы не выбрали изображение!');
    }
    else
    {
        $('form').submit();
    }
}